package com.mailbox.mailbox.controller;

import com.mailbox.mailbox.domain.SecurityUser;
import com.mailbox.mailbox.service.ReplyService;
import com.mailbox.mailbox.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "回复接口")
public class ReplyController {

    @Resource
    ReplyService replyService;

    @ApiOperation(value = "管理员回复留言")
    @PostMapping("/Admin/replyMessage")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "replyContent", value = "回复信息",required = true),
                    @ApiImplicitParam(name = "messageId", value = "留言id",required = true)
            }
    )
   public ResponseResult replyMessage(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser, String replyContent, Integer messageId){
       Integer userId=securityUser.getUserId();
       if(userId==null){
           return ResponseResult.error("登录失效,请重新登录(用户id为空)");
       }
       if(StringUtils.isAnyBlank(replyContent)){
           return ResponseResult.error("回复内容为空");
       }
        if (messageId==null) {
            return ResponseResult.error("留言id为空");
        }
        return  replyService.replyMessage(userId,replyContent,messageId);
   }



    @ApiOperation(value = "管理员修改已经回复过的留言")
    @PutMapping("/Admin/updateReply")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "content", value = "回复信息",required = true),
                    @ApiImplicitParam(name = "replyId", value = "留言id",required = true)
            }
    )
    public ResponseResult updateReply(Integer replyId,String content){
        if(replyId==null){
            return ResponseResult.error("回复id为空");
        }
        if(StringUtils.isAnyBlank(content)){
            return ResponseResult.error("修改的内容为空");
        }
        return replyService.updateReply(replyId,content);
    }

    @ApiOperation(value = "管理员删除回复的留言")
    @DeleteMapping("/Admin/deleteReply")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "replyId", value = "回复id",required = true)
            }
    )
    public ResponseResult deleteReply(Integer replyId){
        if(replyId==null){
            return ResponseResult.error("回复id为空");
        }
        return replyService.deleteReply(replyId);
    }

    @ApiOperation(value = "管理员批量删除回复的留言")
    @DeleteMapping("/Admin/batchDeleteReply")
    @ApiImplicitParam(name = "replyId", value = "回复id", allowMultiple = true, dataTypeClass = List.class, paramType = "query")
    ResponseResult batchDeleteReply(@RequestParam(value = "replyId")List<Integer> replyId){
        return replyService.batchDeleteReply(replyId);
    }



    @ApiOperation(value = "遍历管理员后台回复的信息")
    @GetMapping("/Admin/findAllReply")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数",required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条",required = true)
            }
    )
    ResponseResult findAllReply(Integer pageNow,Integer pageSize){
        return replyService.findAllReply(pageNow, pageSize);
    }

    @ApiOperation(value = "管理员后台模糊查询回复信息")
    @GetMapping("/Admin/findReplyByNameLike")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数",required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条",required = true),
                    @ApiImplicitParam(name = "keyWord", value = "关键字",required = true)
            }
    )
    ResponseResult findReplyByNameLike(Integer pageNow, Integer pageSize,String keyWord){
        if(StringUtils.isAnyBlank(keyWord)){
            return ResponseResult.error("关键词为空");
        }
        return replyService.findReplyByNameLike(pageNow, pageSize, keyWord);
    }

    @ApiOperation(value = "留言页打开回复的详细信息")
    @GetMapping("/Admin/findReplyByMessageId")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "messageId", value = "留言id",required = true)
            }
    )
    ResponseResult findReplyByMessageId(Integer messageId){
        if(messageId==null){
            return ResponseResult.error("留言id为空");
        }
        return replyService.findReplyByMessageId(messageId);
    }
}
